// 语句块

function addArduinoCod (Blockly) {
    Blockly.Arduino.test = function (block) {
        // If/elseif/else condition.
        return '111 \n';
    };
    Blockly.Arduino.serial_band_rate = function (block) {
        return 'serial_band_rate() \n';
    }
	//串口波特率
  //      Blockly.Arduino['hb_serialbegin'] = function(block) {
  //       var dropdown_BURARATES = this.getFieldValue('BURARATES');
	// 	return 'Serial.begin('+dropdown_BURARATES+');\n';
  //  }
	//串口打印字符
  //      Blockly.Arduino['hb_printstr'] = function(block) {
	// 	var value_serialprintss = Blockly.Arduino.valueToCode(this, 'serialprintss', Blockly.Arduino.ORDER_ATOMIC);
	// 	return 'Serial.println('+value_serialprintss.replace(/'/g, '"')+');\n';
  //  }
	//串口打印数字
  //      Blockly.Arduino['hb_printnum'] = function(block) {
	// 	var value_serialprintsss = Blockly.Arduino.valueToCode(this, 'serialprintsss', Blockly.Arduino.ORDER_ATOMIC);
	// 	return 'Serial.println('+value_serialprintsss.replace(/'/g, '')+');\n';
  //  }
  
  



   //lcd配置地址
  Blockly.Arduino['ph20lcdinit'] = function(block) {
    var value_lcdinits = Blockly.Arduino.valueToCode(this, 'lcdinits', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_lcdinit'] = '#include <LiquidCrystal_I2C.h>\n#include <Wire.h>';
    Blockly.Arduino.definitions_['lcdinit'] = 'LiquidCrystal_I2C mylcd('+value_lcdinits.replace(/'/g, "")+',16,2);';
    //Blockly.Arduino.definitions_['setup_lcdinit'] = '';
    return  'mylcd.init();\nmylcd.backlight();\n';
  }
  //lcd光标
  Blockly.Arduino['ph20lcdposition'] = function(block) {
    var value_col = Blockly.Arduino.valueToCode(this, 'col', Blockly.Arduino.ORDER_ATOMIC);
    var value_row = Blockly.Arduino.valueToCode(this, 'row', Blockly.Arduino.ORDER_ATOMIC);
    return  'mylcd.setCursor('+value_col+'-1, '+value_row+'-1);\n';
  }
  //lcd打印
  Blockly.Arduino['ph20lcdprint'] = function(block) {
  var value_lcdprints = Blockly.Arduino.valueToCode(this, 'lcdprints', Blockly.Arduino.ORDER_ATOMIC);
  return  'mylcd.print('+value_lcdprints.replace(/'/g, '"')+');\n';
  }
  //lcd清屏
  Blockly.Arduino['ph20lcdclear'] = function(block) {
  return  'mylcd.clear();\n';
  }
//LED
Blockly.Arduino['ph_led'] = function(block) {
    var dropdown_led = this.getFieldValue('led');
    var dropdown_status = this.getFieldValue('status');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_led'] = 'PH20Port led('+dropdown_led + ");\n";
      // Blockly.Arduino.definitions_['setup_ph_led'] = 'pinMode('+dropdown_led+', OUTPUT);\n';
      //Blockly.Arduino.definitions_['ph_led'] = '';
      var code = dropdown_status=="1"?"led.dWrite1(HIGH);":"led.dWrite1(LOW);";
return code;
}

//激光放射器
Blockly.Arduino['ph_laser'] = function(block) {
  var dropdown_laser = this.getFieldValue('laser');
  var dropdown_laserStatus = this.getFieldValue('laserStatus');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_laser'] = 'PH20Port laser('+dropdown_laser + ");\n";
    // Blockly.Arduino.definitions_['setup_ph_led'] = 'pinMode('+dropdown_led+', OUTPUT);\n';
    //Blockly.Arduino.definitions_['ph_led'] = '';
  var code = dropdown_laserStatus=="1"?"laser.dWrite1(HIGH);\n":"laser.dWrite1(LOW);\n";
  return code;
}
//声音传感器
Blockly.Arduino['ph_vioce'] = function(block) {
    var dropdown_phvioce = this.getFieldValue('phvioce');
    var vioceValue = this.getFieldValue('vioceValue');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_vioce'] = 'PH20Port vioce('+dropdown_phvioce + ");\n";
    var code = vioceValue;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//光敏传感器
Blockly.Arduino['ph_light'] = function(block) {
    var dropdown_phlight = this.getFieldValue('phlight');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_light'] = 'PH20Port light('+dropdown_phlight + ");\n";
    	//Blockly.Arduino.definitions_['ph_led'] = '';
      var code = 'light.aRead1()';
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//触摸传感器
Blockly.Arduino['ph_touch'] = function(block) {
    var dropdown_phtouch = this.getFieldValue('phtouch');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_touch'] = 'PH20Port touch('+dropdown_phtouch + ");\n";
    // Blockly.Arduino.definitions_['setup_ph_touch'] = 'pinMode('+dropdown_phtouch+', INPUT);\n';
    	//Blockly.Arduino.definitions_['ph_led'] = '';
      var code = 'touch.dpRead1()==HIGH';
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//按键传感器
Blockly.Arduino['ph_button'] = function(block) {
    var dropdown_phbutton = this.getFieldValue('phbutton');
    var dropdown_isButton = this.getFieldValue('isButton');
    Blockly.Arduino.definitions_['include_button'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['initButton'] = 'PH20Port button('+ dropdown_phbutton +');\n'
    // Blockly.Arduino.definitions_['setup_ph_button'] = 'pinMode('+dropdown_phbutton+', INPUT);\n';
      //Blockly.Arduino.definitions_['ph_led'] = '';
      var code = '';
      if(dropdown_isButton == 'down'){
        code = 'button.dpRead1() == LOW';
      }else if(dropdown_isButton == 'loosen'){
        code = 'button.dpRead1() == HIGH';
      }else{
        Blockly.Arduino.definitions_['initFlag'] = 'boolean flag;\n'
        Blockly.Arduino.definitions_['timer_run_button'] = 'for( int i = 0 ;i < 3000;i++ ){\n'+
          '   if(button.dpRead1() == LOW){\n'+
            '     delay(1);\n'+
            '      flag = true;\n'+
          '   }else{\n'+
            '     flag = false;\n'+
            '     delay(1);\n'+
            '     break;\n'+
            '   \n'+
            '   }\n'+
            '}\n';

        code = 'flag';
      } 
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//热敏传感器
Blockly.Arduino['ph_thermosensitive'] = function(block) {
    var dropdown_thermosensitive = this.getFieldValue('thermosensitive');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_thermosensitive'] = 'PH20Port thermosensitive('+dropdown_thermosensitive + ");\n";
      // Blockly.Arduino.definitions_['setup_ph_thermosensitive'] = 'pinMode('+dropdown_thermosensitive+', INPUT);';
    var code = 'thermosensitive.aRead1()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//火焰传感器
Blockly.Arduino['ph_fire'] = function(block) {
    var dropdown_fire = this.getFieldValue('fire');
    var dropdown_fireValue = this.getFieldValue('fireValue');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_fire'] = 'PH20Port fire('+dropdown_fire + ");\n";
    var code = 'fire.'+dropdown_fireValue;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//避障传感器
Blockly.Arduino['ph_abstacle'] = function(block) {
  var dropdown_abstacle = this.getFieldValue('abstaclePort');
  var dropdown_abstacleValue = this.getFieldValue('abstacleValue');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_fire'] = 'PH20Port avoidance('+dropdown_abstacle + ");\n";
  var code = 'avoidance.'+dropdown_abstacleValue;
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//土壤湿度传感器
Blockly.Arduino['ph_soil'] = function(block) {
    var dropdown_soil = this.getFieldValue('soil');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_soil'] = 'PH20Port soil('+dropdown_soil + ");\n";
    var code = 'soil.aRead1()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//雨滴传感器
Blockly.Arduino['ph_rain'] = function(block) {
    var dropdown_rain = this.getFieldValue('rain');
    var dropdown_rainValue = this.getFieldValue('rainValue');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_rain'] = 'PH20Port rain('+dropdown_rain + ");\n";
    var code = 'rain.' + dropdown_rainValue;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//水深传感器
Blockly.Arduino['ph_waterdeep'] = function(block) {
    var dropdown_waterdeep = this.getFieldValue('waterdeep');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_waterdeep'] = 'PH20Port waterdeep('+dropdown_waterdeep + ");\n";
    var code = 'waterdeep.aRead1()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//碰撞传感器
Blockly.Arduino['ph_knockswitch'] = function(block) {
    var dropdown_knockswitch = this.getFieldValue('knockswitch');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_knockswitch'] = 'PH20Port knockSwitch('+dropdown_knockswitch + ");\n";
    	//Blockly.Arduino.definitions_['ph_led'] = '';
    var code = 'knockSwitch.dpRead1() == LOW';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//dht11
Blockly.Arduino['ph_dht11'] = function(block) {
    var dropdown_dht11 = this.getFieldValue('dht11');
    var dropdown_temhum = this.getFieldValue('temhum');
    Blockly.Arduino.definitions_['include_ph_dht11'] = '#include "dht11.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_port'] = 'dht11 dht11;\n';
    Blockly.Arduino.definitions_['ph_port'] = 'PH20Port dhtPort(' + dropdown_dht11 + ");\n";
    Blockly.Arduino.definitions_['timer_run_read'] = ' dht11.read(dhtPort.pin1());\n';
    var code = '(float)dht11.'+ dropdown_temhum;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//温度传感器
Blockly.Arduino['ph_ds'] = function(block) {
    var dropdown_ds = this.getFieldValue('ds');
    Blockly.Arduino.definitions_['include_ph_ds'] = '#include "OneWire.h"\n#include "DallasTemperature.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['setup_ph_ds'] = '  dallasTemperature.begin();\n';
    Blockly.Arduino.definitions_['ph_ds'] = 'PH20Port ds18b20(' + dropdown_ds + ');\nOneWire oneWire(ds18b20.pin1());\nDallasTemperature dallasTemperature(&oneWire);\n';
    var code = '';
    return code;
}

//温度传感器WWW
Blockly.Arduino['ph_lm35'] = function(block) {
  var dropdown_lm35 = this.getFieldValue('lm35');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['ph_portLm35'] = 'PH20Port lm35(' + dropdown_lm35 + ");\n";
  Blockly.Arduino.definitions_['include_ps_lm35Value'] = 'int val,lm35Data; \nfloat voltage = 0.0;\n';
  Blockly.Arduino.definitions_['timer_run_lm35'] = " val = lm35.aRead1();\n  voltage = ((float)val/1023);\n  voltage = voltage * 5;\n  lm35Data = voltage * 100;\n";
  // Blockly.Arduino.definitions_['ph_ds'] = 'PH20Port ds18b20(' + dropdown_ds + ');\nOneWire oneWire(ds18b20.pin1());\nDallasTemperature dallasTemperature(&oneWire);\n';
  var code = 'lm35Data';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//温度传感器获取温度  ph_lm35
Blockly.Arduino['ph_readds'] = function(block) {
  Blockly.Arduino.definitions_['timer_run_get'] = ' dallasTemperature.requestTemperatures(); \n';
  var code = 'dallasTemperature.getTempCByIndex(0)';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//RGB超声波初始化
Blockly.Arduino['ph_rgbUltrasonicInit'] = function(block) {
  var dropdown_ulPINS = this.getFieldValue('ph_ulPINS');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_rgbUltrasonic'] = '#include "RgbUltrasonic.h"\nPH20Port rgbUltrasonic('+ dropdown_ulPINS +');\nRgbUltrasonic mRUS04(rgbUltrasonic.pin1(), rgbUltrasonic.pin2());\n';
  var code = '';
  return code;
}

//RGB超声波设置颜色
Blockly.Arduino['ph_rgbultrasonicsetcolorstyle'] = function(block) {
  var rgbultrasonicposition = this.getFieldValue('ph_rgbultrasonicposition');
  var rgbultrasoniccolor = this.getFieldValue('ph_rgbultrasoniccolor');
  var rgbultrasoniccolorstyle = this.getFieldValue('ph_rgbultrasoniccolorstyle');
  var dropdown_color = block.childBlocks_[0].colour_; 
  dropdown_color = dropdown_color.replace("#","0x");
  // if(rgbultrasoniccolor == "RGB_RED") {
  //   rgbultrasoniccolor == "RGB_GREEN";
  // }else if(rgbultrasoniccolor == "RGB_GREEN") {
  //   rgbultrasoniccolor == "RGB_GREEN";
  // }
  var code = 'mRUS04.SetRgbEffect(' + rgbultrasonicposition+', ' + dropdown_color + ', ' + rgbultrasoniccolorstyle + ');\n  delay(1000);\n';
  return code;
}

//RGB超声波获取距离
Blockly.Arduino['ph_rgbultrasonicreaddistance'] = function(block) {
  var code = 'mRUS04.GetUltrasonicDistance()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}


// //温度传感器读取温度
// Blockly.Arduino['ph_readds'] = function(block) {
//       var code = 'sensors.getTempCByIndex(0)';
//       	return [code, Blockly.Arduino.ORDER_ATOMIC];
// }
//气体传感器
Blockly.Arduino['ph_air'] = function(block) {
    var dropdown_air = this.getFieldValue('air');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_air'] = 'PH20Port air('+ dropdown_air + ");\n";
    var dropdown_airValue = this.getFieldValue('airValue');
    var code = 'air.' + dropdown_airValue;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//人体热释电传感器
Blockly.Arduino['ph_body'] = function(block) {
    var dropdown_body = this.getFieldValue('body');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_body'] = 'PH20Port body('+ dropdown_body + ");\n";
    // Blockly.Arduino.definitions_['setup_ph_body'] = 'pinMode('+dropdown_body+', INPUT);';
    	//Blockly.Arduino.definitions_['ph_led'] = '';
    var code = 'body.dpRead1() == HIGH';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//循迹传感器
Blockly.Arduino['ph_track'] = function(block) {
    var dropdown_track = this.getFieldValue('track');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_track'] = 'PH20Port track('+ dropdown_track + ");\n";
      // Blockly.Arduino.definitions_['setup_ph_track'] = 'pinMode('+dropdown_track+', INPUT);';
    	//Blockly.Arduino.definitions_['ph_led'] = '';
      var code = 'track.dpRead1() == LOW';
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//蜂鸣器播放声音选择声音
Blockly.Arduino['ph_buzzerplay2'] = function(block) {
  var dropdown_buzzerinitPort = this.getFieldValue('phBuzzerInitPort');
  var dropdown_songs = this.getFieldValue('ph_songs');
  Blockly.Arduino.definitions_['include_buzzerplay2'] = '#include <Buzzer.h>\n#include <Sounds.h>';
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_PH20PortInit'] = 'PH20Port buzzerplay('+ dropdown_buzzerinitPort +');\n';
  Blockly.Arduino.definitions_['buzzerplay2'] = 'Buzzer mBuzzer = Buzzer(buzzerplay.pin1());';
  if(dropdown_songs===("S_connection")){
    var code = 'mBuzzer._tone(note_E5, 50, 30);\nmBuzzer._tone(note_E6, 55, 25);\nmBuzzer._tone(note_A6, 60, 10);\n';
  }
  if(dropdown_songs===("S_disconnection")){
    var code = 'mBuzzer._tone(note_E5, 50, 30)\n;mBuzzer._tone(note_A6, 55, 25);\nmBuzzer._tone(note_E6, 50, 10);\n';
  }
  if(dropdown_songs===("S_buttonPushed")){
    var code = 'mBuzzer.bendTones (note_E6, note_G6, 1.03, 20, 2);\ndelay(30);\nmBuzzer.bendTones (note_E6, note_D7, 1.04, 10, 2);\n';
  }
  if(dropdown_songs===("S_mode3")){
    var code = 'mBuzzer._tone(note_E6, 50, 100);\nmBuzzer._tone(note_G6, 50, 80);\nmBuzzer._tone(note_D7, 300, 0);\n ';
  }
  if(dropdown_songs===("S_surprise")){
    var code = 'mBuzzer.bendTones(800, 2150, 1.02, 10, 1);\nmBuzzer.bendTones(2149, 800, 1.03, 7, 1);\n';
  }
  if(dropdown_songs===("S_OhOoh")){
    var code = 'for (int i = 880; i < 2000; i = i * 1.04) {\nmBuzzer._tone(note_B5, 5, 10);\n}\n';
  }
  if(dropdown_songs===("S_OhOoh2")){
    var code = 'for (int i = 1880; i < 3000; i = i * 1.03) {\nmBuzzer._tone(note_C6, 10, 10);\n}\n';
  }
  if(dropdown_songs===("S_cuddly")){
    var code = 'mBuzzer.bendTones(700, 900, 1.03, 16, 4);\nmBuzzer.bendTones(899, 650, 1.01, 18, 7);\n';
  }
  if(dropdown_songs===("S_happy")){
    var code = 'mBuzzer.bendTones(1500, 2500, 1.05, 20, 8);\nmBuzzer.bendTones(2499, 1500, 1.05, 25, 8);\n';
  }
  if(dropdown_songs===("S_sad1")){
    var code = 'mBuzzer.bendTones(1600, 3000, 1.02, 2, 15);\n';
  }
  if(dropdown_songs===("S_sad2")){
    var code = 'mBuzzer.bendTones(1600, 4000, 1.02, 2, 20);\nmBuzzer.bendTones(4000, 3000, 1.02, 2, 20);\n';
  }
  if(dropdown_songs===("S_callPolice")){hong
    Blockly.Arduino.definitions_['include_buzzerplay2'] = '#include <Buzzer.h>\n';
    Blockly.Arduino.definitions_['buzzerplay2'] = 'Buzzer buzzer(buzzerplay.pin1());\n';
    var code = 'for(int i = 200; i <= 800; i++)\n {\n    buzzer.tone(i, 10);\n }\n'+                    //Max Frequency hold 1s   
    ' for(int i= 800; i >= 200; i--)\n {\n    buzzer.tone(i, 10);\n }\n';
  }
  if(dropdown_songs===("S_stop")){
    Blockly.Arduino.definitions_['include_buzzerplay2'] = '#include <Buzzer.h>\n';
    Blockly.Arduino.definitions_['buzzerplay2'] = 'Buzzer buzzer(buzzerplay.pin1());\n';
    var code = 'buzzer.noTone();\n';
  }
  return code;
  }


//mpu
Blockly.Arduino['ph_mpu'] = function(block) {
    var dropdown_mpu2 = this.getFieldValue('mpu2');
    Blockly.Arduino.definitions_['setup_ph_mpu'] = '  mpu6050.InitMpu6050();\n';
    Blockly.Arduino.definitions_['include_ph_mpu'] = '#include "Mpu6050Module.h"\nMpu6050Module mpu6050;\n';
    Blockly.Arduino.definitions_['timer_run_mpu'] = ' mpu6050.GetInclination();\n';
    var code = 'mpu6050.'+dropdown_mpu2;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//红外发送
  Blockly.Arduino['ph_irsend'] = function(block) {
      var dropdown_irsend = this.getFieldValue('irsend');
      var value_irdata2 = Blockly.Arduino.valueToCode(this, 'irdata2', Blockly.Arduino.ORDER_ATOMIC);
      // var value_irbyte2 = Blockly.Arduino.valueToCode(this, 'irbyte2', Blockly.Arduino.ORDER_ATOMIC);
      Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
      Blockly.Arduino.definitions_['include_ph_irsend'] = '#include "IRremote.h"\n';
      Blockly.Arduino.definitions_['include_irsend'] = 'PH20Port irsendPort('+ dropdown_irsend + ");\n";
      Blockly.Arduino.definitions_['ph_irsend'] = 'IRremote irsend(irsendPort.pin1());\n';
      var code = 'irsend.sendNEC(' + value_irdata2.replace(/'/g, '') + ');\n';
      return code;
  }
  //普通遥控器按下
Blockly.Arduino['ph_irKeyPress'] = function(block) {
  var dropdown_irKeyPort = this.getFieldValue('irKeyPort');
  Blockly.Arduino.definitions_['include_IRremote'] = '#include "IRremote.h"\n';
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_keyMap'] = '#include <keymap.h>\n';
  var dropdown_Irkey = this.getFieldValue('irkey');
  Blockly.Arduino.definitions_['include_irKeyPress'] = 'PH20Port irKeyPress('+ dropdown_irKeyPort + ");\n";
  Blockly.Arduino.definitions_['include_IRremoteInit'] = 'IRremote ir(irKeyPress.pin1());\n';
  var code = 'ir.getIrKey(ir.getCode(),1) == '+ dropdown_Irkey;
  return [code, Blockly.Arduino.ORDER_ATOMIC];
  }
  //emakefun遥控器按下
  Blockly.Arduino['ph_emakefunIrKeyPress'] = function(block) {
  //var dropdown_DS18B20ss = this.getFieldValue('DS18B20ss');
    var dropdown_irEmakefunPort = this.getFieldValue('irEmakefunPort');
    Blockly.Arduino.definitions_['include_IRremote'] = '#include "IRremote.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_keyMap'] = '#include <keymap.h>\n';
    var dropdown_irEmakefunkey = this.getFieldValue('irEmakefunkey');
    Blockly.Arduino.definitions_['include_irEmakefunKeyPress'] = 'PH20Port irEmakefunKeyPress('+ dropdown_irEmakefunPort + ");\n";
    Blockly.Arduino.definitions_['include_IRremoteInit'] = 'IRremote irEmakefun(irEmakefunKeyPress.pin1());\n';
    var code = 'irEmakefun.getIrKey(irEmakefun.getCode(),2) == '+ dropdown_irEmakefunkey;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  }
//红外接收
Blockly.Arduino['ph_irreceive'] = function(block) {
    var dropdown_irreceive = this.getFieldValue('irreceive');
    Blockly.Arduino.definitions_['setup_ph_irreceive'] = '  ir.enableIRIn();\n';
    Blockly.Arduino.definitions_['include_ph_IRremote'] = '#include "IRremote.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['ph_IRremote'] = 'PH20Port irPort('+dropdown_irreceive+');\nIRremote ir(irPort.pin1());\n';
    var code = 'ir.getCode() != 0';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//红外接收传感器接收的值
Blockly.Arduino['ph_irreceivedata'] = function(block) {
      Blockly.Arduino.definitions_['setup_ph_irreceive'] = '  ir.enableIRIn();\n';
    //var dropdown_track = this.getFieldValue('track');
    //  Blockly.Arduino.definitions_['setup_ph_track'] = 'pinMode('+dropdown_track+', INTPUT);';
    	//Blockly.Arduino.definitions_['ph_led'] = '';
      var code = 'ir.getCode()';
      	return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//红外接收传感器接收下一个值
Blockly.Arduino['ph_irreceivenext'] = function(block) {
  return  'ir.resume();\n';
}
//有源蜂鸣器
Blockly.Arduino['ph_buzzer'] = function(block) {
    var dropdown_buzzer = this.getFieldValue('buzzer');
    var value_buzzers = Blockly.Arduino.valueToCode(this, 'buzzers', Blockly.Arduino.ORDER_ATOMIC);
    var value_time = Blockly.Arduino.valueToCode(this, 'time', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_buzzer'] = '#include "Buzzer.h"\nPH20Port passivebuzzerPort('+ dropdown_buzzer + ");\n"+'Buzzer buzzer('+ dropdown_buzzer + ");\n";
  return  'buzzer.tone(passivebuzzerPort.pin1(),'+value_buzzers+','+value_time+');\n';
}
//ph_passive_buzzer
Blockly.Arduino['ph_buzzer1'] = function(block) {
  var dropdown_buzzer1 = this.getFieldValue('passiveBuzzer');
  // var value_buzzers = Blockly.Arduino.valueToCode(this, 'buzzers', Blockly.Arduino.ORDER_ATOMIC);
  var value_time1 = Blockly.Arduino.valueToCode(this, 'play_time', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_passive_buzzer'] = 'PH20Port buzzer1('+ dropdown_buzzer1 + ");\n";
  return  'for(int i=0;i<'+value_time1 +';i++)\n{\nbuzzer1.dWrite1(HIGH);\n}\n';
}
//直流电机
Blockly.Arduino['ph_motor'] = function(block) {
  var dropdown_motor = this.getFieldValue('motor');
  var dropdown_direction = this.getFieldValue('direction');
  var value_motorSpeed = Blockly.Arduino.valueToCode(this, 'motorSpeed', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['includemotor'] = 'PH20Port motor('+ dropdown_motor + ");\n";
  if(dropdown_direction === 'forward'){
    return  'motor.aWrite1('+ value_motorSpeed +');\nmotor.aWrite2(0);\n';
  }else{
    return  'motor.aWrite2('+ value_motorSpeed +');\nmotor.aWrite1(0);\n';
  }
}

//双电机驱动
Blockly.Arduino['ph_dcMotorDriver'] = function(block) {
  var dropdown_driverPin = this.getFieldValue('driverPin');
  var dropdown_motor1Direction = this.getFieldValue('motor1');
  var dropdown_motor2Direction = this.getFieldValue('PINS2');
  var dropdown_pwm1 = this.getFieldValue('pwm1');
  var dropdown_pwm2 = this.getFieldValue('pwm2');
  Blockly.Arduino.definitions_['dcmotor2'] = ' void motorModule2(int IN1, int IN2, int pinPwm, int speed){\n    pinMode(IN1, OUTPUT);\n    pinMode(IN2, OUTPUT);\n    if(speed>=0){\n        digitalWrite(IN1, 1);\n        digitalWrite(IN2, 0);\n    }else{\n        digitalWrite(IN1, 0);\n        digitalWrite(IN2, 1);\n    }\n    analogWrite(pinPwm, abs(speed));\n}\n';
  return  'motorModule2('+dropdown_PINS1+', '+dropdown_PINS2+','+dropdown_pwm+', '+value_dcmotors2+');\n';
  }


//继电器
Blockly.Arduino['ph_relay'] = function(block) {
    var dropdown_relay = this.getFieldValue('relay');
    var dropdown_status2 = this.getFieldValue('status2');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_relay'] = 'PH20Port relay('+ dropdown_relay + ");\n";
    var code = 'relay.dWrite1('+dropdown_status2+');\n';
    return code;
}
//  tm1650数码管
Blockly.Arduino['ph_tm1650'] = function(block) {
    // var dropdown_tm1650 = this.getFieldValue('tm1650');
    var dropdown_status3 = this.getFieldValue('status3');
  //var value_irdata2 = Blockly.Arduino.valueToCode(this, 'irdata2', Blockly.Arduino.ORDER_ATOMIC);
  //var value_irbyte2 = Blockly.Arduino.valueToCode(this, 'irbyte2', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_ph_tm1650'] = '#include <TM1650.h>\n#include <Wire.h>\n';
    Blockly.Arduino.definitions_['setup_ph_tm1650'] = 'Wire.begin();\n   tm_4display.init();\n';
  Blockly.Arduino.definitions_['ph_tm1650'] = 'TM1650 tm_4display;\n';
    var code = ' tm_4display.'+dropdown_status3+';\n';
      return code;
}
//tm1650数码管显示
Blockly.Arduino['ph_tm1650display'] = function(block) {
  var value_tm1650displays = Blockly.Arduino.valueToCode(this, 'tm1650displays', Blockly.Arduino.ORDER_ATOMIC);
  //var value_irdata2 = Blockly.Arduino.valueToCode(this, 'irdata2', Blockly.Arduino.ORDER_ATOMIC);
  //var value_irbyte2 = Blockly.Arduino.valueToCode(this, 'irbyte2', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_ph_tm1650'] = '#include <TM1650.h>\n#include <Wire.h>\n';
    Blockly.Arduino.definitions_['setup_ph_tm1650'] = 'Wire.begin();\n  tm_4display.init();\n';
    Blockly.Arduino.definitions_['ph_tm1650'] = 'TM1650 tm_4display;\n';
    var code = 'tm_4display.displayString('+value_tm1650displays.replace(/'/g, '"')+');\n';
      return code;
}

Blockly.Arduino['ph_tm1650displayNumber'] = function(block) {
  var value_tm1650displays = Blockly.Arduino.valueToCode(this, 'tm1650displaysNumber', Blockly.Arduino.ORDER_ATOMIC);
  
  //var value_irdata2 = Blockly.Arduino.valueToCode(this, 'irdata2', Blockly.Arduino.ORDER_ATOMIC);
  //var value_irbyte2 = Blockly.Arduino.valueToCode(this, 'irbyte2', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_ph_tm1650'] = '#include <TM1650.h>\n#include <Wire.h>\n';
    Blockly.Arduino.definitions_['setup_ph_tm1650'] = 'Wire.begin();\n  tm_4display.init();\n';
    Blockly.Arduino.definitions_['ph_tm1650'] = 'TM1650 tm_4display;\n';
    var code = 'tm_4display.displayNumber(' + value_tm1650displays + ');\n';
      return code;
}


//tm1650数码管显示小数点
Blockly.Arduino['ph_tm1650displaypoint'] = function(block) {
  var dropdown_tm1650displaypoint = this.getFieldValue('tm1650displaypoint');
  var dropdown_tm1650displaypointnum = this.getFieldValue('tm1650displaypointnum');
    Blockly.Arduino.definitions_['include_ph_tm1650'] = '#include <TM1650.h>\n#include <Wire.h>\n';
    Blockly.Arduino.definitions_['setup_ph_tm1650'] = 'Wire.begin();\n  tm_4display.init();\n';
    Blockly.Arduino.definitions_['ph_tm1650'] = 'TM1650 tm_4display;\n';
    var code = 'tm_4display.setDot('+dropdown_tm1650displaypoint+','+dropdown_tm1650displaypointnum+');\n';
      return code;
}
//tm1637初始化
Blockly.Arduino['ph_tm1637'] = function(block) {
  var dropdown_tm1637 = this.getFieldValue('tm1637');
  //var dropdown_tm1650displaypointnum = this.getFieldValue('tm1650displaypointnum');
    Blockly.Arduino.definitions_['include_ph_timerOne'] = '#include "TimerOne.h"\n';
    Blockly.Arduino.definitions_['include_ph_tm1637'] = '#include "TM1637.h"\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ON'] = '#define ON 1\n';
    Blockly.Arduino.definitions_['include_OFF'] = '#define OFF 0\n';
    Blockly.Arduino.definitions_['setup_ph_tm1637'] = 'tm1637.begin();\n';
 
    Blockly.Arduino.definitions_['setup_ph_initialize'] = 'Timer1.initialize(500000);\n';
    Blockly.Arduino.definitions_['setup_ph_attachInterrupt'] = 'Timer1.attachInterrupt(TimingISR);\n';
    Blockly.Arduino.definitions_['ph_tm1637'] = 'PH20Port tm1637Port('+dropdown_tm1637 +');\n';
    Blockly.Arduino.definitions_['ph_TimeDisp'] = 'String TimeDisp = "";\n';
    Blockly.Arduino.definitions_['ph_ClockPoint'] = 'unsigned char ClockPoint = 1;\n';
    Blockly.Arduino.definitions_['ph_Update'] = 'unsigned char Update;\n';
    Blockly.Arduino.definitions_['ph_halfsecond'] = 'unsigned char halfsecond = 0;\n';
    Blockly.Arduino.definitions_['ph_second'] = 'unsigned char second;\n';
    Blockly.Arduino.definitions_['ph_minute'] = 'unsigned char minute;\n';
    Blockly.Arduino.definitions_['ph_hour'] = 'unsigned char hour;\n';
    Blockly.Arduino.definitions_['ph_display'] = 'TM1637  tm1637(tm1637Port.pin1(),tm1637Port.pin2());\n';
    Blockly.Arduino.definitions_['ph_timingISR'] = 'void TimingISR()\n'+
    '{\n'+
    '  halfsecond ++;\n'+
    '  Update = ON;\n'+
    '  if(halfsecond == 2){\n'+
    '    second ++;\n'+
    '    if(second == 60)\n'+
    '    {\n'+
    '      minute ++;\n'+
    '      if(minute == 60)\n'+
    '      {\n'+
    '        hour ++;\n'+
    '        if(hour == 24)\n'+
    '        hour = 0;\n'+
    '        minute = 0;\n'+
    '      }\n'+
    '      second = 0;\n'+
    '    }\n'+
    '    halfsecond = 0;\n'+  
    '  }\n'+
    '  ClockPoint = (~ClockPoint) & 0x01;\n'+
    '}\n';
    Blockly.Arduino.definitions_['ph_timeUpdate'] ='void TimeUpdate(void)\n'+
    '{\n'+
    '  if(ClockPoint)tm1637.setColonOn(false);\n'+
    '  else tm1637.setColonOn(true);\n'+
    '  TimeDisp = hour / 10;\n'+
    '  TimeDisp += hour % 10;\n'+
    '  TimeDisp += minute / 10;\n'+
    '  TimeDisp += minute % 10;\n'+
    '  Update = OFF;\n'+
    '}\n';
    var code = '';
      return code;
}
//tm1637设置亮度
Blockly.Arduino['ph_tm1637setlight'] = function(block) {
    var value_tm1637setlights = Blockly.Arduino.valueToCode(this, 'tm1637setlights', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'tm1637.setBacklight('+value_tm1637setlights+');\n';
      return code;
}
//tm1637显示
Blockly.Arduino['ph_tm1637display'] = function(block) {
    var value_tm1637displays = Blockly.Arduino.valueToCode(this, 'tm1637displays', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'tm1637.print('+value_tm1637displays.replace(/'/g, '"')+');\n';
      return code;
}

//tm1637显示
Blockly.Arduino['ph_tm1637displayPoint'] = function(block) {
  var dropdown_tm1637displaypointnum = this.getFieldValue('tm1637displaypointnum');
  var code = 'tm1637.setColonOn('+dropdown_tm1637displaypointnum+');\n';
    return code;
}
//tm1637显示时间
Blockly.Arduino['ph_tm1637displaytime'] = function(block) {
    var value_tm1637displaytime1 = Blockly.Arduino.valueToCode(this, 'tm1637displaytime1', Blockly.Arduino.ORDER_ATOMIC);
      var value_tm1637displaytime2 = Blockly.Arduino.valueToCode(this, 'tm1637displaytime2', Blockly.Arduino.ORDER_ATOMIC);
  //var dropdown_tm1650displaypointnum = this.getFieldValue('tm1650displaypointnum');
    // Blockly.Arduino.definitions_['include_ph_tm1637'] = '#include <SevenSegmentExtended.h>\n';
    Blockly.Arduino.definitions_['setup_minute'] = 'minute = '+value_tm1637displaytime2+';\n';
    Blockly.Arduino.definitions_['setup_hour'] = 'hour = '+value_tm1637displaytime1+';\n';
    // Blockly.Arduino.definitions_['ph_display'] = 'SevenSegmentExtended  display(tm1637.pin1(),tm1637.pin2());\n';
    
    var code ='  if(Update == ON)\n'+
    '  {\n'+
    '    TimeUpdate();\n'+
    '    tm1637.print(TimeDisp);\n'+
    '  }\n';
      return code;
}
//tm1637清屏
Blockly.Arduino['ph_tm1637clear'] = function(block) {
  //  var value_tm1637setlights = Blockly.Arduino.valueToCode(this, 'tm1637setlights', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'tm1637.clear();\n';
      return code;
}
//rgb
Blockly.Arduino['ph_rgb'] = function(block) {
    var value_rgb1 = Blockly.Arduino.valueToCode(this, 'rgb1', Blockly.Arduino.ORDER_ATOMIC);
      var value_rgb2 = Blockly.Arduino.valueToCode(this, 'rgb2', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_rgb = this.getFieldValue('rgb');
  //Blockly.Arduino.definitions_['setup_ph_rgb'] = '';
    Blockly.Arduino.definitions_['include_ph_rgb'] = '#include <Adafruit_NeoPixel.h>\n';
    Blockly.Arduino.definitions_['ph_rgb'] = 'Adafruit_NeoPixel  rgb_display= Adafruit_NeoPixel('+value_rgb1+','+dropdown_rgb+',NEO_GRB + NEO_KHZ800);\n';
    var code = 'rgb_display.begin();\n rgb_display.setBrightness('+value_rgb2+');\n';
      return code;
}
//rgb颜色
Blockly.Arduino['ph_rgbcolor'] = function(block) {
  var value_rgbPort = this.getFieldValue('rgb');
  var dropdown_rgbColor = block.childBlocks_[0].colour_; 
  var colorStr = dropdown_rgbColor.colorRgb();
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_port'] = 'PH20Port rgbPort('+ value_rgbPort +');\n';
  var red_color = 0;
  var blue_color = 0;
  var green_color = 0;
  //Blockly.Arduino.definitions_['setup_ph_rgb'] = '';
  //  Blockly.Arduino.definitions_['include_ph_rgb'] = '#include <Adafruit_NeoPixel.h>\n';
  //  Blockly.Arduino.definitions_['ph_rgb'] = 'Adafruit_NeoPixel  rgb_display= Adafruit_NeoPixel('+value_rgb1+','+dropdown_rgb+',NEO_GRB + NEO_KHZ800);\n';
  if (colorStr) {
      var colorArr = colorStr.split(",");
      red_color = colorArr[0];
      green_color = colorArr[1];
      blue_color = colorArr[2];
   }

  var code = 'rgbPort.aWrite3('+ blue_color +');\n rgbPort.aWrite4('+ red_color + ');\n rgbPort.aWrite5('+ green_color +');\n';
  return code;
}

String.prototype.colorRgb = function(){
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
	var sColor = this.toLowerCase();
	if(sColor && reg.test(sColor)){
		if(sColor.length === 4){
			var sColorNew = "#";
			for(var i=1; i<4; i+=1){
				sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));	
			}
			sColor = sColorNew;
		}
		//处理六位的颜色值
		var sColorChange = [];
		for(var i=1; i<7; i+=2){
			sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));	
		}
		return  sColorChange.join(",") ;
	}else{
		return sColor;	
	}
};

//rgb环初始化
Blockly.Arduino['ph_ringRgbColorInit'] = function(block) {
  var value_rgbPort = this.getFieldValue('rgbRing');
  
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_ring'] = '#include "RGBLed.h"\nPH20Port rgbRing('+ value_rgbPort +');\n';
  // Blockly.Arduino.definitions_['include_mrgb'] = '#include "RGBLed.h"\nPH20Port rgbRing('+ value_rgbPort +');\n';
  // Blockly.Arduino.definitions_['setup_begin'] = 'pixels.begin();';
  // Blockly.Arduino.definitions_['include_ph_pixels'] = 'Adafruit_NeoPixel pixels = Adafruit_NeoPixel(12, A0, NEO_GRB + NEO_KHZ800);';
  return '';
}
// 设置RGB环亮度
Blockly.Arduino['ph_setRingRgbLight'] = function(block) {
  var value_rgbLight = Blockly.Arduino.valueToCode(this, 'rgbLight', Blockly.Arduino.ORDER_ATOMIC);
   var code = 'mRgb.setBrightness('+ value_rgbLight +');\n';
  return code;
}
//rgb环
Blockly.Arduino['ph_ringRgbColor'] = function(block) {
  var value_total = Blockly.Arduino.valueToCode(this, 'total', Blockly.Arduino.ORDER_ATOMIC);
  var value_start = Blockly.Arduino.valueToCode(this, 'start', Blockly.Arduino.ORDER_ATOMIC);
  var value_end = Blockly.Arduino.valueToCode(this, 'end', Blockly.Arduino.ORDER_ATOMIC);
  var value_red = 0;
  var value_green = 0;
  var value_blue = 0;
  var value_lightTime = Blockly.Arduino.valueToCode(this, 'lightTime', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_mrgb'] = 'RGBLed mRgb(rgbRing.pin1(),'+ value_total+');\n';
  Blockly.Arduino.definitions_['include_ph_time'] = 'int delayval = '+ value_lightTime +';';
  var dropdown_rgbColor = block.childBlocks_[4].colour_; 
  var colorStr = dropdown_rgbColor.colorRgb();
  if (colorStr) {
    var colorArr = colorStr.split(",");
    value_red = colorArr[0];
    value_green = colorArr[1];
    value_blue = colorArr[2];
 }
  // Blockly.Arduino.definitions_['include_ph_pixels'] = 'Adafruit_NeoPixel pixels = Adafruit_NeoPixel('+value_total+', rgbRing.pin1(), NEO_GRB + NEO_KHZ800);';
  var code = 'for(int i='+value_start +'-1;i<'+ value_end +';i++){\n    mRgb.setColorAt(i, ' + value_red + ', ' + value_green + ', ' + value_blue + '); \n    mRgb.show(); \n'+
    '    delay(delayval);\n}';
  return code;
}
// 摇杆
Blockly.Arduino['ph_rock'] = function(block) {
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  
    var dropdown_rock = this.getFieldValue('rock');
    var dropdown_rock2 = this.getFieldValue('rock2');
    var code = '';
    Blockly.Arduino.definitions_['include_ph_rock'] = 'PH20Port rock('+ dropdown_rock +');\n';
    // dropdown_rock2 = dropdown_rock2 == "D2"?2:dropdown_rock2;
    // if(dropdown_rock2 === "D2"){
    //   code = 'digitalRead(2)';
    // }else{
    //   code = 'analogRead('+dropdown_rock2+')';
    // }
    // Blockly.Arduino.definitions_['setup_ph_rock'] = '  pinMode('+dropdown_rock2+', INPUT);\n';
      //Blockly.Arduino.definitions_['ph_led'] = '';  
      
    return [dropdown_rock2, Blockly.Arduino.ORDER_ATOMIC];
}
//钢琴模块
Blockly.Arduino['ph_piano'] = function(block) {
    var dropdown_piano = this.getFieldValue('piano');
    var dropdown_piano2 = this.getFieldValue('piano2');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_piano'] = '#include <BS818A.h>\nPH20Port piano('+ dropdown_piano + ");\n";
    Blockly.Arduino.definitions_['setup_ph_piano'] = '  BS.InitBS818A(piano.pin2(), piano.pin1());\n';
    Blockly.Arduino.definitions_['ph_piano'] = 'BS818A BS;\n';
    var code = 'BS.PressBsButton('+dropdown_piano2+')';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//旋转电位器
Blockly.Arduino['ph_resistance'] = function(block) {
    var dropdown_resistance = this.getFieldValue('resistance');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_piano'] = 'PH20Port resistance('+ dropdown_resistance + ");\nfloat voltage=0,data;";
    Blockly.Arduino.definitions_['timer_run_resistance'] = 'data=resistance.aRead1();\nvoltage=(data/1023)*5;\n';
      // Blockly.Arduino.definitions_['setup_ph_resistance'] = 'pinMode('+dropdown_resistance+', INPUT);\n';
    var code = 'voltage';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//滑动电位器
Blockly.Arduino['ph_slipresistance'] = function(block) {
    var dropdown_slipresistance = this.getFieldValue('slipresistance');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_piano'] = 'PH20Port slidePotentiometer('+ dropdown_slipresistance + ");\nfloat i=0,data;\nint j = 0;\n";
    // Blockly.Arduino.definitions_['setup_ph_slipresistance'] = 'pinMode('+dropdown_slipresistance+', INPUT);\n';
    Blockly.Arduino.definitions_['timer_run_slip'] = ' data=slidePotentiometer.aRead1();\n  i=data/1023;\n  j=(1-i)*100;\n';
    var code = 'j';
    return [code, Blockly.Arduino.ORDER_ATOMIC];j
}
//红绿灯
Blockly.Arduino['ph_trafficlights'] = function(block) {
    var trafficlights  = this.getFieldValue('trafficlights');
    // var dropdown_trafficlights1 = this.getFieldValue('trafficlights1');
    var dropdown_trafficlightsR = this.getFieldValue('trafficlights2');
    var dropdown_trafficlightsG = this.getFieldValue('trafficlights3');
    var dropdown_trafficlightsY = this.getFieldValue('trafficlights4');
    var lightTime = Blockly.Arduino.valueToCode(this, 'lightTime', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_trafficlights'] = 'PH20Port trafficlights('+ trafficlights + ");\n";

    var code = 'trafficlights.dWrite3('+dropdown_trafficlightsG +');\n trafficlights.dWrite4('+dropdown_trafficlightsY +');\n trafficlights.dWrite5('+dropdown_trafficlightsR +');\n delay('+ lightTime +');\n';
      return code;
}
//流水灯
Blockly.Arduino['ph_waterlights'] = function(block) {
    var dropdown_waterlights = this.getFieldValue('waterlights');
    var dropdown_waterlights1 = this.getFieldValue('waterlights1');
  Blockly.Arduino.definitions_['setup_ph_waterlights'] = 'pinMode('+dropdown_waterlights+', OUTPUT);\n';
    var code = 'digitalWrite('+dropdown_waterlights+','+dropdown_waterlights1+');\n';
      return code;
}
//8x8点阵屏初始化
Blockly.Arduino['ph_max'] = function(block) {
    var dropdown_max = this.getFieldValue('max');
    var value_maxs = Blockly.Arduino.valueToCode(this, 'maxs', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['setup_ph_max'] = '  ledmatrix.init();\n  ledmatrix.setIntensity(1);\n  ledmatrix.clearMatrix();\n';
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_max'] = '#include <MaxMatrix.h>\nPH20Port matrix('+ dropdown_max +');';
    Blockly.Arduino.definitions_['ph_max'] = 'MaxMatrix ledmatrix(matrix.pin5(),matrix.pin4(),matrix.pin3(),'+value_maxs+');\n';
    return '';
}
//8x8点阵屏单点亮或灭
Blockly.Arduino['ph_maxpoint'] = function(block) {
    var dropdown_draw = this.getFieldValue('draw');
    // var value_maxpoint1 = Blockly.Arduino.valueToCode(this, 'maxpoint1', Blockly.Arduino.ORDER_ATOMIC);
    // var value_maxpoint2 = Blockly.Arduino.valueToCode(this, 'maxpoint2', Blockly.Arduino.ORDER_ATOMIC);
    return 'ledmatrix.drawShape(ledmatrix.getMouthShape('+dropdown_draw+'));';
}

//8x8点阵屏单点亮
Blockly.Arduino['ph_x8leddisplay'] = function(block) {
  var dropdown_draw = this.getFieldValue('draws');
  // var value_maxpoint1 = Blockly.Arduino.valueToCode(this, 'maxpoint1', Blockly.Arduino.ORDER_ATOMIC);
  // var value_maxpoint2 = Blockly.Arduino.valueToCode(this, 'maxpoint2', Blockly.Arduino.ORDER_ATOMIC);
  return 'ledmatrix.writeFull('+dropdown_draw+');';
}

	//手势传感器
  Blockly.Arduino['ph_handle_gesture'] = function(block) {
    // Blockly.Arduino.definitions_['breaks_value'] = 'int value = apds.readGesture();\n';
    Blockly.Arduino.definitions_['define_ph_handle_gesture_status'] = 'int gestureStatus;\n';
    
    var dropdown_status = this.getFieldValue('gestureStatus');
    var code = 'gestureStatus == '+ dropdown_status;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  }

  //手势传感器初始化
  Blockly.Arduino['ph_handleGestureInit'] = function(block) {
    Blockly.Arduino.definitions_['include_ph_handle_gesture'] = '#include <Wire.h>\n#include "SparkFun_APDS9960.h"\n';
    Blockly.Arduino.definitions_['define_ph_handle_gesture'] = 'SparkFun_APDS9960 apds = SparkFun_APDS9960();\n';
    Blockly.Arduino.definitions_['setup_ph_handle_gesture_sensor'] = '  apds.init();\n  apds.enableGestureSensor(true);\n';
    // var code = 'apds.enableGestureSensor(true)';
    return "";
  }

  //手势传感器是否获取到手势
  Blockly.Arduino['ph_gesture'] = function(block) {
    var code = 'apds.isGestureAvailable()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  }

  //手势传感器状态
  Blockly.Arduino['ph_gestureStatus'] = function(block) {
    // var dropdown_status = this.getFieldValue('gestureStatus');
    // Blockly.Arduino.definitions_['timer_run_handle_status'] = 'gestureStatus = apds.readGesture();\n';
    var code = 'gestureStatus = apds.readGesture();\n' ;
    return code;
  }

  
   //旋转编码器初始化
   Blockly.Arduino['ph_ec11Init'] = function(block) {
    var dropdown_rotaryPort = this.getFieldValue('rotary');
    // var dropdown_rotaryPort = this.getFieldValue('rotary');
    Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
    Blockly.Arduino.definitions_['include_ph_ec11Init'] = 'PH20Port rotaryEncoder('+ dropdown_rotaryPort +');\n';
    Blockly.Arduino.definitions_['define_ph_ec11Val'] = 'long position, oldposition;\n';
    Blockly.Arduino.definitions_['setup_ph_ec11Port'] = 'pinMode(rotaryEncoder.pin1(), INPUT);\n  pinMode(rotaryEncoder.pin2(), INPUT);\n  pinMode(rotaryEncoder.pin3(), INPUT);\n  attachInterrupt(1, read_quadrature, CHANGE);\n';
    return '';
  }
   //旋转编码器
   Blockly.Arduino['ph_ec11'] = function(block) {
    Blockly.Arduino.definitions_['include_ec11'] = 'void read_quadrature(){\n'+  
    '  if (digitalRead(ENCODER_A_PIN) == LOW){\n'+    
    '    if (digitalRead(ENCODER_B_PIN) == LOW)\n'+ 
    '      position++;\n'+ 
    '  }\n'+ 
    '  else {\n'+ 
    '    if (digitalRead(ENCODER_B_PIN) == LOW)\n'+ 
    '      position--;\n'+ 
    '  }\n'+ 
    '  oldposition = position;\n'+ 
    '}\n';
    var code = "position";
    return [code, Blockly.Arduino.ORDER_ATOMIC];;
  }

  //旋转编码器按钮是否被按下
  Blockly.Arduino['ph_ec11ButtonPushDown'] = function(block) {
    // Blockly.Arduino.definitions_['breaks_value'] = 'int value = apds.readGesture();\n';
    var code = 'rotaryEncoder.dRead5() == LOW';
    return [code, Blockly.Arduino.ORDER_ATOMIC];;
  }

    //磁簧开关传感器是否检测到磁场
    Blockly.Arduino['ph_reed_switch'] = function(block) {
      var dropdown_reedPort = this.getFieldValue('reedPort');
      Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
      Blockly.Arduino.definitions_['include_ph_reed_switch'] = 'PH20Port reedSwitch('+ dropdown_reedPort +');\n';
      // Blockly.Arduino.definitions_['breaks_value'] = 'int value = apds.readGesture();\n';
      var code = 'reedSwitch.dRead1()==LOW';
      return [code, Blockly.Arduino.ORDER_ATOMIC];;
    }

      //磁簧开关传感器是否检测到磁场
      Blockly.Arduino['ph_tilt'] = function(block) {
        var dropdown_tilt = this.getFieldValue('tilt');
        Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
        Blockly.Arduino.definitions_['include_tilt'] = 'PH20Port tilt('+ dropdown_tilt +');\n';
        // Blockly.Arduino.definitions_['breaks_value'] = 'int value = apds.readGesture();\n';
        var code = 'tilt.dRead1()==LOW';
        return [code, Blockly.Arduino.ORDER_ATOMIC];;
      }

 
//灰度传感器器
Blockly.Arduino['ph_grayscale'] = function(block) {
  var dropdown_grayscale = this.getFieldValue('grayscale');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_grayscale'] = 'PH20Port grayscale('+ dropdown_grayscale +');\n';
  var code ='grayscale.aRead1()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];;
}

//震动传感器器
Blockly.Arduino['ph_shock'] = function(block) {
  var dropdown_shock = this.getFieldValue('shockPin');
  var dropdown_shockValue = this.getFieldValue('shockValue');
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_shock' + dropdown_shock] = 'PH20Port shock('+ dropdown_shock +');\n';
  var code ='shock.'+dropdown_shockValue;
  return [code, Blockly.Arduino.ORDER_ATOMIC];;
}

//超声波读值
Blockly.Arduino['ph_ultrasonic'] = function(block) {
  var dropdown_trig = this.getFieldValue('trig');
  var dropdown_echo = this.getFieldValue('echo');
  Blockly.Arduino.definitions_['setup_ultrasonic' + dropdown_trig] = '  pinMode('+ dropdown_trig + ', OUTPUT);\n  pinMode('+ dropdown_echo + ', INPUT);\n';
  // var dropdown_shockValue = this.getFieldValue('shockValue');
  // Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  // Blockly.Arduino.definitions_['include_ph_ultrasonic' + dropdown_shock] = 'PH20Port ultrasonic('+ dropdown_shock +');\nfloat distance;\n';
  Blockly.Arduino.definitions_['timer_run_ultrasonic'] = ' digitalWrite('+ dropdown_trig +', LOW);\n  delayMicroseconds(2);\n  digitalWrite('+ dropdown_trig +', HIGH);\n  delayMicroseconds(10);\n'+
  '  digitalWrite('+ dropdown_trig +', LOW);\n';
  var code ='pulseIn(' + dropdown_echo + ', HIGH) / 58.00';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//舵机
Blockly.Arduino['ph_servo'] = function(block) {
  var dropdown_servopins = this.getFieldValue('phservopins');
  // Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_ph_servo'] = '#include <Servo.h>\n';
  Blockly.Arduino.definitions_['include_ph_servo' + dropdown_servopins] = 'Servo servo_'+ dropdown_servopins + ';\n';
  Blockly.Arduino.definitions_['setup_servoInit' + dropdown_servopins] = '  servo_'+ dropdown_servopins + '.attach('+dropdown_servopins +');\n';
  var value_servoangle = Blockly.Arduino.valueToCode(this, 'phservoangle', Blockly.Arduino.ORDER_ATOMIC);//
    return 'servo_'+dropdown_servopins+'.write('+value_servoangle+');\n';
  }

  //串口波特率
Blockly.Arduino['ph_serialbegin'] = function(block) {
	var dropdown_SerialS1 = this.getFieldValue('SerialS1');
var dropdown_serialbegins = this.getFieldValue('serialbegins');
//Blockly.Arduino.definitions_['setup_serialbegin'] = '';
return  ''+dropdown_SerialS1+'.begin(' + dropdown_serialbegins + '); \n';
}
//串口打印字符不换行
Blockly.Arduino['ph_serialprintstring'] = function(block) {
	var dropdown_SerialS22 = this.getFieldValue('SerialS22');
var value_serialprints1 = Blockly.Arduino.valueToCode(this, 'serialprints1', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS22+'.print(' + value_serialprints1.replace(/'/g, '"') + '); \n';
}
//串口打印字符
Blockly.Arduino['ph_serialprint'] = function(block) {
	var dropdown_SerialS2 = this.getFieldValue('SerialS2');
var value_serialprints = Blockly.Arduino.valueToCode(this, 'serialprints', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS2+'.println(' + value_serialprints.replace(/'/g, '"') + '); \n';
}
//串口打印数字不换行
Blockly.Arduino['ph_serialprintnum1'] = function(block) {
		var dropdown_SerialS33 = this.getFieldValue('SerialS33');
var value_serialprintsss = Blockly.Arduino.valueToCode(this, 'serialprintsss', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS33+'.print(' + value_serialprintsss + '); \n';
}
//串口打印数字
Blockly.Arduino['ph_serialprintnum'] = function(block) {
		var dropdown_SerialS3 = this.getFieldValue('SerialS3');
var value_serialprintss = Blockly.Arduino.valueToCode(this, 'serialprintss', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS3+'.println(' + value_serialprintss + '); \n';
}
//串口打印16进制
Blockly.Arduino['ph_serialprint16num'] = function(block) {
	var dropdown_SerialS4 = this.getFieldValue('SerialS4');
var value_serialprint16nums = Blockly.Arduino.valueToCode(this, 'serialprint16nums', Blockly.Arduino.ORDER_ATOMIC);
return  ''+dropdown_SerialS4+'.println('+value_serialprint16nums+',HEX);\n';
}
//串口有数据可读
Blockly.Arduino['ph_serialava'] = function(block) {
	var dropdown_SerialS = this.getFieldValue('SerialS');
var code = ''+dropdown_SerialS+'.available() > 0';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//串口读取字节
Blockly.Arduino['ph_serialread'] = function(block) {
		var dropdown_Serials = this.getFieldValue('Serials');
 var code = ''+dropdown_Serials+'.read()';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
}
//软串口
Blockly.Arduino['ph_softserial'] = function(block) {
var dropdown_TX1 = this.getFieldValue('TX1');
var dropdown_RX1 = this.getFieldValue('RX1');
Blockly.Arduino.definitions_['include_softserial'] = '#include <SoftwareSerial.h>';
Blockly.Arduino.definitions_['softserial'] = 'SoftwareSerial softwareSerial = SoftwareSerial('+dropdown_RX1+', '+dropdown_TX1+');';
var code = '';
 return code;
}
//串口中断
Blockly.Arduino['ph_serial_event'] = function(block) {

  var serial_select = this.getFieldValue('Serials');
    var funcName = 'attachPinInterrupt_fun_' + serial_select;
    var branch = Blockly.Arduino.statementToCode(this, 'DO');
    var code2 = ' void '+serial_select+'Event() {\n' + branch + '}\n';
    Blockly.Arduino.definitions_['func_'+funcName+''] = code2;
    return "";

}
//串口清空缓存数据
Blockly.Arduino['ph_serial_flush'] = function(block) {

  var serial_select = this.getFieldValue('Serials');
    return ''+serial_select+'.flush();\n';

}
//串口读取字符串直到
Blockly.Arduino['ph_serial_read'] = function(block) {
	var serial_select = this.getFieldValue('Serials');
var value_serialprints2 = Blockly.Arduino.valueToCode(this, 'serialprints2', Blockly.Arduino.ORDER_ATOMIC);
 var code = ''+serial_select+'.readStringUntil('+value_serialprints2+')';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//串口读取整数小数
Blockly.Arduino['ph_serialreadint'] = function(block) {
		var dropdown_Serials9 = this.getFieldValue('Serials9');
    var dropdown_intfloat = this.getFieldValue('intfloat');
 var code = ''+dropdown_Serials9+'.'+dropdown_intfloat+'';
 return [code, Blockly.Arduino.ORDER_ATOMIC];
}

//初始化矩阵键盘
Blockly.Arduino['ph_matrixKeyboardInit'] = function(block) {
var dropdown_port = this.getFieldValue('matrixKeyBoardPort');
Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
Blockly.Arduino.definitions_['include_TTP229'] = '#include "TTP229.h"\n';
Blockly.Arduino.definitions_['include_TTP229Init'] = 'PH20Port matrixKeyPort('+dropdown_port +');\nTTP229 mTTP229;\n';
Blockly.Arduino.definitions_['setup_TTP229'] = '  mTTP229.initTTP229(matrixKeyPort.pin1(), matrixKeyPort.pin2());\n';
return '';
}


//矩阵键盘 按下按钮 状态判断
Blockly.Arduino['ph_matrixKeyboardFunc'] = function(block) {
    var dropdown_keyBoardValue = this.getFieldValue('ph_keyBoardValue');
    var dropdown_keyBoardStatus = this.getFieldValue('ph_keyBoardCorrespondValue');
    Blockly.Arduino.definitions_['timer_run_matrixkeyBoard'] = ' String key_name = mTTP229.GetKeyMap();\n  char * result = (char *)key_name.c_str();\n ';
    var code ='';
    if(dropdown_keyBoardStatus === 'press'){
      code = 'result == "'+ dropdown_keyBoardValue +'"' ;
    }else if(dropdown_keyBoardStatus === 'release'){
      code = 'result != "'+ dropdown_keyBoardValue +'"' ;
    }else{
      Blockly.Arduino.definitions_['initMatrixKeyBoardFlag'] = 'boolean matrixKeyBoardFlag;\n'
        Blockly.Arduino.definitions_['timer_run_matrixKeyBoard3'] = '  for( int i = 0 ;i < 3000;i++ ){\n'+
          '   if(result == "'+ dropdown_keyBoardValue +'"){\n'+
            '     delay(1);\n'+
            '      matrixKeyBoardFlag = true;\n'+
          '   }else{\n'+
            '     matrixKeyBoardFlag = false;\n'+
            '     delay(1);\n'+
            '     break;\n'+
            '   \n'+
            '   }\n'
        code = 'matrixKeyBoardFlag';
    }
    return  [code, Blockly.Arduino.ORDER_ATOMIC];;
  }
  
  //红外接收传感器接收的值
Blockly.Arduino['ph_getMatrixKeyboardValue'] = function(block) {
  Blockly.Arduino.definitions_['timer_run_matrixkeyBoard'] = ' String key_name = mTTP229.GetKeyMap();\n  char * result = (char *)key_name.c_str();\n';
  var code = "result";
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}


//初始化双路电机驱动
Blockly.Arduino['ph_dcMotorDriver'] = function(block) {
  var dropdown_driverPin = this.getFieldValue('driverPin');
  // var dropdown_motor1 = this.getFieldValue('motor1');
  // var dropdown_motor2 = this.getFieldValue('motor2');
  var dropdown_phDcMotors1 = Blockly.Arduino.valueToCode(this, 'phDcMotors1', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_phDcMotors2 = Blockly.Arduino.valueToCode(this, 'phDcMotors2', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_motorDriver'] = 'PH20Port motorDriver('+dropdown_driverPin +');\n';
 var code = '';
  if(dropdown_phDcMotors1 > 0){
    code +=  '  motorDriver.aWrite1(abs('+ dropdown_phDcMotors1 +'));\n  motorDriver.aWrite2(0);\n';
  }else{
    code +=  '  motorDriver.aWrite2(abs('+ dropdown_phDcMotors1 +'));\n  motorDriver.aWrite1(0);\n';
  }
  if(dropdown_phDcMotors2 > 0){
    code +=  '  motorDriver.aWrite3(abs('+ dropdown_phDcMotors2 +'));\n  motorDriver.aWrite4(0);\n';
  }else{
    code +=  '  motorDriver.aWrite4(abs('+ dropdown_phDcMotors2 +'));\n  motorDriver.aWrite3(0);\n';
  }
  return code;
  }


  //初始化ds1302
Blockly.Arduino['ph_initds1302'] = function() {
  var dropdown_port =  this.getFieldValue('ds1302Port');
  Blockly.Arduino.definitions_['include_initds1302'] = '#include <DS1302.h>';
  Blockly.Arduino.definitions_['include_PH20Port'] = '#include "PH20Port.h"\n';
  Blockly.Arduino.definitions_['include_DS1302' + dropdown_port] = 'PH20Port ds1302(' + dropdown_port + ');\n';
  Blockly.Arduino.definitions_['initds1302'] = 'DS1302 myRTC(ds1302.pin5(), ds1302.pin4(), ds1302.pin3());\n';
  return '';
  };
  //获取ds1302年月日时分秒
  Blockly.Arduino['ph_getds1302date'] = function() {
  var dropdown_DATES =  this.getFieldValue('DATES');
  var code = 'myRTC.'+dropdown_DATES+'()';
   return [code, Blockly.Arduino.ORDER_ATOMIC];
  };
  //ds1302设置时分秒
  Blockly.Arduino['ph_setds1302time'] = function() {
  var value_hour = Blockly.Arduino.valueToCode(this, 'hour', Blockly.Arduino.ORDER_ATOMIC);
  var value_minute = Blockly.Arduino.valueToCode(this, 'minute', Blockly.Arduino.ORDER_ATOMIC);
  var value_second = Blockly.Arduino.valueToCode(this, 'second', Blockly.Arduino.ORDER_ATOMIC);
  return 'myRTC.setTime('+ value_hour + ', ' + value_minute + ', ' + value_second + ');\n';
  };
  //ds设置年月日
  Blockly.Arduino['ph_setds1302date'] = function() {
  var value_year = Blockly.Arduino.valueToCode(this, 'year', Blockly.Arduino.ORDER_ATOMIC);
  var value_month = Blockly.Arduino.valueToCode(this, 'month', Blockly.Arduino.ORDER_ATOMIC);
  var value_day = Blockly.Arduino.valueToCode(this, 'day', Blockly.Arduino.ORDER_ATOMIC);
  return 'myRTC.setDate(' + value_day +', '+ value_month +', '+ value_year +');\n';
  };

    /*悍马初始化
    Blockly.Arduino['hb_setup'] = function(block) {
        Blockly.Arduino.definitions_['include_hb_setup'] = '#include <ProtocolParser.h>\n#include <BluetoothHandle.h>\n#include <Keymap.h>\n#include <debug.h>\n#include <Hummerbot.h>';
		Blockly.Arduino.definitions_['hb_setup'] = 'ProtocolParser *mProtocol = new ProtocolParser();\nHummerbot hbot(mProtocol, EM_IN1_PIN, EM_IN2_PIN, EM_IN3_PIN, EM_IN4_PIN);';
        return 'Serial.begin(9600);\nhbot.init();\nhbot.SetSpeed(0);\n';
   }
   //方向，速度
       Blockly.Arduino['hb_move'] = function(block) {
        var dropdown_DIRECTIONS = this.getFieldValue('DIRECTIONS');
		var value_Speed = Blockly.Arduino.valueToCode(this, 'Speed', Blockly.Arduino.ORDER_ATOMIC);
		return 'hbot.SetSpeed('+value_Speed+');\nhbot.Move('+dropdown_DIRECTIONS+');\n';
   }
   //舵机初始化
          Blockly.Arduino['hb_servo'] = function(block) {
        var dropdown_SERVOPINS = this.getFieldValue('SERVOPINS');
		var value_StandardServoDegree = Blockly.Arduino.valueToCode(this, 'StandardServoDegree', Blockly.Arduino.ORDER_ATOMIC);
		return 'hbot.SetServoPin('+dropdown_SERVOPINS+');\nhbot.mRgbUltrasonic->SetServoBaseDegree('+value_StandardServoDegree+');\nhbot.mRgbUltrasonic->SetServoDegree(90);\n';
   }
      //设置舵机角度
    Blockly.Arduino['hb_servodegree'] = function(block) {
		var value_servodegree= Blockly.Arduino.valueToCode(this, 'servodegree', Blockly.Arduino.ORDER_ATOMIC);
		return 'hbot.mRgbUltrasonic->SetServoDegree('+value_servodegree+');\n';
   }
   //电机初始化
             Blockly.Arduino['hb_motor'] = function(block) {
        var dropdown_LNPINS = this.getFieldValue('LNPINS');
        var dropdown_LPPINS = this.getFieldValue('LPPINS');
        var dropdown_RNPINS = this.getFieldValue('RNPINS');
        var dropdown_RPPINS = this.getFieldValue('RPPINS');
		return 'hbot.SetMotorPin('+dropdown_LNPINS+','+dropdown_LPPINS+','+dropdown_RNPINS+','+dropdown_RPPINS+');\n';
   }
    //转弯角度，速度
          Blockly.Arduino['hb_turn'] = function(block) {
		var value_TurnAngle = Blockly.Arduino.valueToCode(this, 'TurnAngle', Blockly.Arduino.ORDER_ATOMIC);
		var value_TurnSpeed = Blockly.Arduino.valueToCode(this, 'TurnSpeed', Blockly.Arduino.ORDER_ATOMIC);
		return 'hbot.SetSpeed('+value_TurnSpeed+');\nhbot.Drive('+value_TurnAngle+');\n';
   }
         //加速
		Blockly.Arduino['hb_speedup'] = function(block) {
		return 'hbot.SpeedUp(10);\n';
   }
      //减速
       Blockly.Arduino['hb_speeddown'] = function(block) {
		return 'hbot.SpeedDown(10);\n';
   }
   //获取当前速度
         Blockly.Arduino['hb_getcurrentspeed'] = function(block) {
		var code =  'hbot.GetSpeed()';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
     //红外接收初始化
      Blockly.Arduino['hb_irReceiveInit'] = function(block) {
    Blockly.Arduino.definitions_['hb_irReceiveInit'] = 'byte irKeyCode;';

		return 'hbot.IrInit();\n';
   }
     //红外遥控器按键被按下？
       Blockly.Arduino['hb_irKeyPressed'] = function(block) {
		var code =  'irKeyCode = hbot.mIrRecv->getCode()';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   	 //红外遥控器的什么键被按下？
                        Blockly.Arduino['hb_irKeyPress'] = function(block) {
        var dropdown_IRKEYS = this.getFieldValue('IRKEYS');
		var code =  'hbot.mIrRecv->getIrKey(hbot.mIrRecv->getCode(),2) == '+dropdown_IRKEYS+'';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   	  //rgb超声波模块引脚
                Blockly.Arduino['hb_ultrasonicpin'] = function(block) {
        var dropdown_ULTRASONICPINS = this.getFieldValue('ULTRASONICPINS');
        var dropdown_RGBPINS = this.getFieldValue('RGBPINS');
		return 'hbot.SetRGBUltrasonicPin('+dropdown_ULTRASONICPINS+','+dropdown_RGBPINS+');\n';
   }
   	//读取超声波距离
                           Blockly.Arduino['hb_readUltrasonicDistance'] = function(block) {
        var dropdown_ULTRASONICDITANCES = this.getFieldValue('ULTRASONICDITANCES');
		var code =  'hbot.GetUltrasonicValue('+dropdown_ULTRASONICDITANCES+')';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   	  //设置rgb的颜色
                   Blockly.Arduino['hb_RGB'] = function(block) {
        var dropdown_RGB = this.getFieldValue('RGB');
        var dropdown_RGBCOLORS = this.getFieldValue('RGBCOLORS');
		var dropdown_RGBCOLORSstyle = this.getFieldValue('RGBCOLORSstyle');
		//return 'hbot.SetRgbColor((E_RGB_INDEX)'+dropdown_RGB+','+dropdown_RGBCOLORS+');\n';
		return 'hbot.mRgbUltrasonic->SetRgbEffect((E_RGB_INDEX)'+dropdown_RGB+','+dropdown_RGBCOLORS+','+dropdown_RGBCOLORSstyle+');\n'
   }
   	//红外避障引脚
                      Blockly.Arduino['hb_irAvoidance'] = function(block) {
        var dropdown_IRAVOIDANCELEFTPINS = this.getFieldValue('IRAVOIDANCELEFTPINS');
        var dropdown_IRAVOIDANCERIGHTPINS = this.getFieldValue('IRAVOIDANCERIGHTPINS');
		return 'hbot.SetInfraredAvoidancePin('+dropdown_IRAVOIDANCELEFTPINS+','+dropdown_IRAVOIDANCERIGHTPINS+');\n';
   }
   	//检测障碍物
                              Blockly.Arduino['hb_readIrAvoidanceDistance'] = function(block) {
        var dropdown_IRAVOIDANCEDITANCES = this.getFieldValue('IRAVOIDANCEDITANCES');
		var code =  'hbot.GetInfraredAvoidanceValue(2)=='+dropdown_IRAVOIDANCEDITANCES+'';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
    //读取红外避障值
   Blockly.Arduino['hb_readIrAvoidanceDistances'] = function(block) {
        var dropdown_IRAVOIDANCEDITANCESS = this.getFieldValue('IRAVOIDANCEDITANCESS');
		var code =  'hbot.GetInfraredAvoidanceValue('+dropdown_IRAVOIDANCEDITANCESS+')';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   	  //追光模块引脚
                         Blockly.Arduino['hb_photosensitivePin'] = function(block) {
        var dropdown_PHOTOSENSITIVELEFTPINS = this.getFieldValue('PHOTOSENSITIVELEFTPINS');
        var dropdown_PHOTOSENSITIVERIGHTPINS = this.getFieldValue('PHOTOSENSITIVERIGHTPINS');
		return 'hbot.SetPhotosensitivePin('+dropdown_PHOTOSENSITIVELEFTPINS+','+dropdown_PHOTOSENSITIVERIGHTPINS+');\n';
   }
     //获取光线弱度值（值越大光越弱）
                              Blockly.Arduino['hb_getPhotosensitive'] = function(block) {
        var dropdown_PHOTOSENSITIVEVALUE = this.getFieldValue('PHOTOSENSITIVEVALUE');
		var code = 'hbot.GetPhotosensitive('+dropdown_PHOTOSENSITIVEVALUE+') / 10';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
       //设置ps2引脚
         Blockly.Arduino['hb_PS2Pin'] = function(block) {
        var dropdown_CLKPINS = this.getFieldValue('CLKPINS');
        var dropdown_CMDPINS = this.getFieldValue('CMDPINS');
        var dropdown_ATTPINS = this.getFieldValue('ATTPINS');
        var dropdown_DATPINS = this.getFieldValue('DATPINS');
         Blockly.Arduino.definitions_['hb_PS2Pin'] = 'byte Ps2xStatus, Ps2xType;';
		return 'hbot.SetPs2xPin('+dropdown_CLKPINS+','+dropdown_CMDPINS+','+dropdown_ATTPINS+','+dropdown_DATPINS+');\nPs2xType = hbot.mPs2x->readType();\n';
   }
		//ps2开始读取键值
         Blockly.Arduino['hb_ps2ready'] = function(block) {
		var code =  'hbot.mPs2x->read_gamepad(false, 0);\n';
		return code;
   }

    //ps2按键被按下？
      Blockly.Arduino['hb_ps2KeyPressed'] = function(block) {
		var code =  'hbot.mPs2x->ButtonDataByte()';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //ps2的什么键被按下？
        Blockly.Arduino['hb_ps2KeyPress'] = function(block) {
        var dropdown_PS2KEYS = this.getFieldValue('PS2KEYS');
		var code =  'hbot.mPs2x->Button('+dropdown_PS2KEYS+')';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
	//接收蓝牙数据
        Blockly.Arduino['hb_receiveBluetoothData'] = function(block) {
		var code =  'mProtocol->RecevData();\n';
		return code;
   }
	//接收到了蓝牙数据
          Blockly.Arduino['hb_receivedBluetoothData'] = function(block) {
			var code =  'mProtocol->ParserPackage()';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }

	//蓝牙数据类型是
         Blockly.Arduino['hb_bluetoothstyle'] = function(block) {
        var dropdown_BLUESTYLES = this.getFieldValue('BLUESTYLES');
		var code =  'mProtocol->GetRobotControlFun()=='+dropdown_BLUESTYLES+'';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
	//蓝牙操作键按下
       Blockly.Arduino['hb_bluetoothKeyPressed'] = function(block) {
        var dropdown_BLUEKEYS = this.getFieldValue('BLUEKEYS');
		var code =  'mProtocol->GetBluetoothButton()=='+dropdown_BLUEKEYS+'';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
	//蓝牙获取的角度
         Blockly.Arduino['hb_bluetoothgetdegree'] = function(block) {
		var code =  'mProtocol->GetRobotDegree()';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //设置当前速度为蓝牙获取的速度
        Blockly.Arduino['hb_bluetoothgetspeed'] = function(block) {
		var code =  'hbot.SetSpeed(mProtocol->GetRobotSpeed());\n';
		return code;
   }
   //红外循迹模块引脚
        Blockly.Arduino['hb_tracking'] = function(block) {
        var dropdown_TRACKINGLEFT = this.getFieldValue('TRACKINGLEFT');
        var dropdown_TRACKINGMID = this.getFieldValue('TRACKINGMID');
        var dropdown_TRACKINGRIGHT = this.getFieldValue('TRACKINGRIGHT');
		var code =  'hbot.SetInfraredTracingPin('+dropdown_TRACKINGLEFT+','+dropdown_TRACKINGMID+','+dropdown_TRACKINGRIGHT+');\n';
		return code;
   }
  	//循迹状态是？
         Blockly.Arduino['hb_trackingstate'] = function(block) {
        var dropdown_TRACKINGSTATE = this.getFieldValue('TRACKINGSTATE');
		var code =  'hbot.mInfraredTracing->GetValue() == '+dropdown_TRACKINGSTATE+'';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //定义一个偏离黑线时的循迹状态变量
       Blockly.Arduino['hb_definetrackingold'] = function(block) {
        var dropdown_TRACKINGSTATEOLDSET = this.getFieldValue('TRACKINGSTATEOLDSET');
		var code =  'static byte old;\n';
		return code;
   }
   		//偏离黑线时的循迹状态是
       Blockly.Arduino['hb_trackingstateold'] = function(block) {
		var dropdown_TRACKINGSTATEOLD = this.getFieldValue('TRACKINGSTATEOLD');
         var code =  'old == '+dropdown_TRACKINGSTATEOLD+'';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   	//设置下次偏离黑线前的循迹状态是
         Blockly.Arduino['hb_settrackingstateold'] = function(block) {
        var dropdown_TRACKINGSTATEOLDSET = this.getFieldValue('TRACKINGSTATEOLDSET');
		var code =  'old = '+dropdown_TRACKINGSTATEOLDSET+';\n';
		return code;
   }
    //火焰传感器模块引脚
        Blockly.Arduino['hb_firepin'] = function(block) {
        var dropdown_FIRELEFT = this.getFieldValue('FIRELEFT');
        var dropdown_FIRERIGHT = this.getFieldValue('FIRERIGHT');
        var dropdown_SOUND = this.getFieldValue('SOUND');
		var code =  'hbot.SetFireVoiceSensorPin( '+dropdown_FIRELEFT+' , '+dropdown_FIRERIGHT+' , '+dropdown_SOUND+' );\n';
		return code;
   }
    //风扇引脚
	   Blockly.Arduino['hb_fun'] = function(block) {
        var dropdown_FUN = this.getFieldValue('FUN');
		var code =  'hbot.SetFanPin('+dropdown_FUN+', 0);\n';
		return code;
   }
	  //左或右，有无火源 hb_firefun
	  Blockly.Arduino['hb_firefun'] = function(block) {
		 var dropdown_FIREFUNDIRECTION = this.getFieldValue('FIREFUNDIRECTION');
		 var dropdown_ISFIRE = this.getFieldValue('ISFIRE');
         var code =  '(digitalRead('+dropdown_FIREFUNDIRECTION+') == '+dropdown_ISFIRE+')';
		return [code, Blockly.Arduino.ORDER_ATOMIC];
	   }
	  //开关风扇
	     Blockly.Arduino['hb_openfirefun'] = function(block) {
        var dropdown_FUNSS = this.getFieldValue('FUNSS');
		var code =  'hbot.SetFanPin(3, '+dropdown_FUNSS+');\n';
		return code;
   }
   //nrf初始化
   Blockly.Arduino.hb_nrfinit = function () {
      var code = 'hbot.SetNrf24L01Pin();\n';
       return code;
   };
   //nrf数据已准备
   Blockly.Arduino.hb_nrfdataready = function () {
       var code = 'hbot.mNrf24L01->dataReady()';
       return [code, Blockly.Arduino.ORDER_ATOMIC];
   };
   //nrf接收数据
   Blockly.Arduino.hb_nrfgetdata = function () {
       //var value_nrfadd = Blockly.Arduino.valueToCode(this, 'nrfadd', Blockly.Arduino.ORDER_ATOMIC);
       var code = 'hbot.mNrf24L01->getData(value);\nmProtocol->RecevData(value, 12);\n';
       return code;
   };
   //获取到nrf数据包？
   Blockly.Arduino.hb_nrfgetpackage = function () {
       var code = 'mProtocol->ParserPackage()';
       return [code, Blockly.Arduino.ORDER_ATOMIC];
   }
   //nrf数据类型是？下拉框
   Blockly.Arduino.hb_nrfdatafun = function () {
       var dropdown_datafun = this.getFieldValue('datafun');
       var code = 'mProtocol->GetRobotControlFun() == '+dropdown_datafun+'';
       return [code, Blockly.Arduino.ORDER_ATOMIC];
   };
   //nrf获取的角度
   Blockly.Arduino.hb_nrfgetdegree = function () {
       var code = 'hbot.Drive(mProtocol->GetRobotDegree());\n';
       return code;
   };
   //nrf获取的速度
   Blockly.Arduino.hb_nrfgetspeed = function () {
       var code = 'hbot.SetSpeed(mProtocol->GetRobotSpeed());\n';
      return code;
   };

*/









    return Blockly;
}

module.exports = addArduinoCod ;
